using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Elementum
{
    class ConsoleMechine : Switch
    {
        Animation _console;
        Animation _consoleText;
        int _count;

        public ConsoleMechine(string id, World world, Stage stage)
            : base(world, stage)
        {
            ConsoleID = id;

            _body.SetUserData("machine");

            if (!StageManager.Save.StageStateTable[StageManager.CurrentStateID].MechineStateTable.ContainsKey(ConsoleID))
            {
                MechineState mechineState = new MechineState();
                try
                {
                    StageManager.Save.StageStateTable[StageManager.CurrentStateID].MechineStateTable.Add(ConsoleID, mechineState);
                }
                catch (ArgumentException)
                {
                    throw new Exception("An Mechine with Key = " + ConsoleID + " already exists.");
                }
            }
            _consoleText = new Animation("images/items/mechineNumber");
            _consoleText.LayerDepth = 0.8f;
            _console = new Animation("images/items/console_body");
            _console.LayerDepth = 0.8f;

            CurrentAnimation = _console;
            Size = new Vector2(128, 128);

            _isActive = false;
            //_isActive = true;
        }

        private Point Point(int p, int p_2)
        {
            throw new NotImplementedException();
        }

        public string ConsoleID
        {
            get;
            set;
        }

        public override void Update(int delta)
        {
            _consoleText.Update(delta);
            _console.Update(delta);
            _count++;
            if (_count >= 180)
                _count = 0;
            float ang = (float)Math.Sin(_count * PI / 180);
            _consoleText.Alpha = ang;
            if (StageManager.Save.MechineList.Count() == 3 && !_isActive)
                _isActive = true;
        }

        public override void OnDraw(SpriteBatch spriteBatch, Matrix transform)
        {
            _transform = transform;
            int leftC = 3 - StageManager.Save.MechineList.Count();
            Rectangle area = new Rectangle();

            switch (leftC)
            {
                case 1:
                    area = new Rectangle(0, 0, 18, 25);//1
                    break;
                case 2: area = new Rectangle(18, 0, 19, 25);//2
                    break;
                case 3: area = new Rectangle(37, 0, 18, 26);//3
                    break;
            }

            if (GameClass.Random.Next(64) < 10)
                area = new Rectangle(0, 89, 36, 35);//mechine

            _console.OnDraw(spriteBatch, Position, transform);
            if (!_isActive)
                _consoleText.OnDraw(spriteBatch, Position - new Vector2(0, 15), transform, area);

        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            if (contactBody.GetUserData() == "player")
            {
                if (_isActive)
                {
                    ActivedActions();
                }
                else
                {
                    NuActivedActions();
                }
            }
        }

        private void NuActivedActions()
        {
            Transition tra;
            tra = new TitleTransition("images/system/consoleOffline", 0, 1, 10, 60);
            tra.Register(this.GameStage);
            tra = new TitleTransition("images/system/consoleOffline", 1, 1, 55, 120);
            tra.Register(this.GameStage);
            tra = new TitleTransition("images/system/consoleOffline", 1, 0, 115, 165);
            tra.Register(this.GameStage);
        }

        private void ActivedActions()
        {
            Transition tra;
            string textString = "images/system/console_text";
            Animation text;
            text = new Animation(textString, new Rectangle(2, 88, 591, 67), new Point(591, 67));
            tra = new TitleTransition(text, new Vector2(360, 120), 0, 1, 20, 90);
            tra.Register(this.GameStage);
            text = new Animation(textString, new Rectangle(2, 88, 591, 67), new Point(591, 67));
            tra = new TitleTransition(text, new Vector2(360, 120), 1, 1, 90, 370);
            tra.Register(this.GameStage);

            text = new Animation(textString, new Rectangle(2, 157, 613, 48), new Point(613, 48));
            tra = new TitleTransition(text, new Vector2(380, 120 + 80), 0, 1, 120, 200);
            tra.Register(this.GameStage);
            text = new Animation(textString, new Rectangle(2, 157, 613, 48), new Point(613, 48));
            tra = new TitleTransition(text, new Vector2(380, 120 + 80), 1, 1, 200, 370);
            tra.Register(this.GameStage);

            text = new Animation(textString, new Rectangle(2, 2, 636, 84), new Point(636, 84));
            tra = new TitleTransition(text, new Vector2(390, 120 + 160), 0, 1, 210, 280);
            tra.Register(this.GameStage);
            text = new Animation(textString, new Rectangle(2, 2, 636, 84), new Point(636, 84));
            tra = new TitleTransition(text, new Vector2(390, 120 + 160), 1, 1, 280, 370);
            tra.Register(this.GameStage);


            //tra = new TwinkleTransition(290, 450);
            //tra.Register(this.GameStage);
            //tra = new AlphaTransition("images/system/cont", 0, 1, 470, 600);
            //tra.Register(this.GameStage);


            tra = new DefauldTransition(0, 0, 360, 400);//600   700
            tra.Register(this.GameStage);
            Stage0 stage0 = (Stage0)this.GameStage;
            tra.TransitionDoneAction += StageClearTransition;
            tra.TransitionDoneAction += stage0.OpenStone;
            StageManager.Save.MechineList.Add(ConsoleID);
        }

        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
            //base.EndContactAction(contact, contactBody, normal);
        }

        public void StageClearTransition()
        {
            this.GameStage.TransitionList.Clear();
        }

    }
}
